<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('放款日期')" prop="returnVisitDate">
            <el-date-picker
              v-model="form.returnVisitDate"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('放款日期')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('放款金额') + $t('金额单位')" prop="lendersMoney">
            <Money v-model="form.lendersMoney" :placeholder="$t('放款金额') + $t('金额单位')" unit="w" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('还款账户')" prop="repaymentAccount">
            <el-input v-model="form.repaymentAccount" type="textarea" :placeholder="$t('还款账户')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('放款凭证')" prop="fileList">
            <div v-if="form.fileList !== null && form.fileList !== '' && form.fileList !== undefined && form.fileList.length > 0" style="width: 100%;">
              <el-dropdown>
                <span :title="form.fileList[0].name" style="cursor: pointer;color: #3da4fc">
                  <i class="el-icon-paperclip" style="margin-right:5px;" />{{ form.fileList[0].name }}
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item com @click.native="onlinePreview(form.fileList[0])">{{ $t('预览') }}</el-dropdown-item>
                  <el-dropdown-item @click.native="downLoadFileOpens(form.fileList[0].id)">{{ $t('下载') }}</el-dropdown-item>
                  <el-dropdown-item @click.native="deleteFile(form)">{{ $t('删除') }}</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </div>
            <div v-if="form.fileList === null || form.fileList === '' || form.fileList === undefined || form.fileList.length <= 0">
              <uploadSingle
                ref="uploadSingle"
                :data-type="'deal_financing_lending'"
                :business-id="autoBusinessId($route.query.id, form)"
                upload-style="color: #fff;background-color: #67c23a;cursor: pointer;border-radius: 4px;border: 1px solid #67c23a;width: 65px;text-align:center"
                :upload-btn-name="$t('上传')"
                @onFileSuccess="onFileSuccess"
                @okPostUploadHook="okPostUploadHook()"
              />
            </div>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!--    <div slot="footer" class="dialog-footer" style="text-align: right;">-->
    <!--      &lt;!&ndash;保存 取消按钮&ndash;&gt;-->
    <!--      <save-btn has-permi="deal:financingLending" />-->
    <!--    </div>-->

    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <el-button @click="cancel">{{ $t('取消') }}</el-button>
      <el-button type="primary" :disabled="disabledUploading" @click="submitForm(true)">{{ $t('保存') }}</el-button>
    </div>
    <OnlinePreview ref="OnlinePreview" />
  </div>
</template>

<script>
    import { saveDealFinancingLending, getDealFinancingLending } from '@/api/deal/financingProjects/dealFinancingLending'
    import uploadSingle from '@/components/File/Upload/single.vue'
    import { delFile } from '@/api/file/fileBase'
    import { downLoadFileOpen } from '@/utils/zipdownload'
    export default {
      components: {
        uploadSingle
      },
        data() {
            return {
              disabledUploading: false,
                // 表单参数
                form: {
                  fileList: []
                },
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                returnVisitDate: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                lendersMoney: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                repaymentAccount: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ]
                }
            }
        },
        created() {
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getDealFinancingLending(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    dealId: this.$route.query.id,
                    returnVisitDate: undefined,
                    lendersMoney: undefined,
                    repaymentAccount: undefined,
                    delFlag: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
                if (this.form.id) {
                  this.closeForm()
                }
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveDealFinancingLending(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                }).catch(() => {
                    this.loading = false
                })
           },
          onFileSuccess(that, file) {
            this.$nextTick(() => {
              this.form.fileList = [{ id: file.fileId, name: file.fileName }]
            })
            this.disabledUploading = false
          },
          okPostUploadHook() {
            this.disabledUploading = true
          },
          // 预览附件
          onlinePreview(row) {
            this.$refs.OnlinePreview.onlinePreviewFile(row.id)
          },
          deleteFile(row) {
            this.loading = true
            delFile(row.fileList[0].id).then(response => {
              if (response.code === 200) {
                row.fileList = null
                this.msgSuccess(response.msg)
              } else {
                this.msgError(response.msg)
              }
              this.loading = false
            })
          },
          downLoadFileOpens(id) {
            downLoadFileOpen(id)
          }
        }
    }
</script>
